package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("guru")
public class GuruController {

    @Autowired
    private GuruService guruService;

    //下拉框
    @ResponseBody
    @RequestMapping("select")
    public List<Guru> show(){
        return guruService.show();
    }

    //展示
    @LogAnnotation(content = "展示大师",type = "select")
    @ResponseBody
    @RequestMapping("show")
    public Map showAll(int page,int limit,String guruName){
        Map map = new HashMap();
        PageInfo<Guru> pageInfo = guruService.showAll(page, limit, guruName);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }

    //上传文件
    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws IOException {
        Map map = new HashMap();
        String s1 = UUID.randomUUID().toString() + file.getOriginalFilename();
        file.transferTo(new File("G:\\JAVA\\IDEAWorkspace\\cmfzdemo\\src\\main\\webapp\\img",s1));
        map.put("image","/img/"+s1);
        System.out.println("s1 = " + s1);
        return map;
    }

    //添加
    @LogAnnotation(content = "添加大师",type = "add")
    @ResponseBody
    @RequestMapping("add")
    public Map add(Guru guru){
        Map map = new HashMap();
        try {
            guruService.add(guru);
            map.put("isAdd",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd",false);
        }
        return map;
    }


    //修改状态
    @LogAnnotation(content = "修改大师状态",type = "updateStatus")
    @ResponseBody
    @RequestMapping("updateStatus")
    public Map updateStatus(int guruId,int guruStatus){
        Map map = new HashMap();
        try {
            guruService.updateStatus(guruId, guruStatus);
            map.put("isUpdateStatus",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdateStatus",false);
        }
        return map;
    }


    //批量添加
    @ResponseBody
    @RequestMapping("insertMany")
    public Map insertMany(MultipartFile file) throws Exception {
        Map map = new HashMap();
        try {
            //导入参数对象
            ImportParams params = new ImportParams();
            //定义标题占多少行
            params.setTitleRows(1);
            //定义表头占多少行
            params.setHeadRows(1);
            /**
             * 参数1 读取文件的流
             * 参数2 被导入的数据对应的实体类类对象
             * 参数3 导入参数对象
             */
            InputStream inputStream = file.getInputStream();

            List<Guru> objects = ExcelImportUtil.importExcel(inputStream, Guru.class, params);

            guruService.addMany(objects);
            map.put("isAddMany",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAddMany",false);
        }
        return map;
    }



    //全部下载
    @ResponseBody
    @RequestMapping("downloadFile")
    public void downloadFile(HttpServletResponse response) throws IOException {
        List<Guru> gurus = guruService.downloadAll();
        /**
         * 参数1 导出参数对象  标题和表名定义
         * 参数2 被导出的实体类的类对象
         * 参数3 所有的上师的数据
         */
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("全部上师数据", "gurus"), Guru.class, gurus);

        //        2.设置响应头
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("guru.xlsx", "utf-8"));
        response.setContentType("application/vnd.ms-excel");

        workbook.write(response.getOutputStream());

    }


}
